package com.qzxy.premiumhotelsystem.mapper;

import com.qzxy.premiumhotelsystem.domain.entity.Comments;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qzxy.premiumhotelsystem.domain.vo.CommentVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
* @author 14912
* @description 针对表【comments】的数据库操作Mapper
* @createDate 2024-09-12 15:55:14
* @Entity com.qzxy.premiumhotelsystem.domain.pojo.Comments
*/

@Mapper
public interface CommentsMapper extends BaseMapper<Comments> {

    /**
     * 根据房间号查询评论
     * @param room_number
     * @return
     */

    @Select("select comments.* , guests.nickname,guests.img_url,rooms.room_number,rooms.room_type_id from comments " +
            "left join guests on comments.guest_id = guests.guest_id " +
            "left join rooms on rooms.room_id = comments.room_id " +
            "where rooms.room_number = #{room_number}")
    List<CommentVo> searchAllByRoomId(String room_number);


    @Select("select comment_id from comments " +
            "left join guests on comments.guest_id = guests.guest_id " +
            "left join rooms on rooms.room_id = comments.room_id " +
            "where room_number = #{roomNumber}")
    List<String> searchAllCommentsByRoomId(String room_number);

    /**
     * 新增评论
     * @param comments
     * @return
     */
    @Insert("insert into comments(guest_id,room_id,parent_id,comment) values (#{guestId},#{roomId},#{parentId},#{comment})")
    boolean create(Comments comments);

    /**
     * 根据房间号查询一级评论
     * @param room_number
     * @return
     */
    @Select("select comments.* , guests.nickname,guests.img_url, rooms.room_id,rooms.room_number from comments " +
            "left join guests on comments.guest_id = guests.guest_id " +
            "left join rooms on rooms.room_id = comments.room_id " +
            "where room_number = #{roomNumber} and parent_id = 0 ")
    List<CommentVo> searchAllByRoomIdAndParentId(String room_number);

    @Select("select c1.comment_id, c1.guest_id, c1.room_id, c3.room_number,c3.room_type_id,c1.parent_id,c1.comment,c1.create_time, c2.img_url,c2.nickname " +
            "from comments c1 " +
            "left join guests c2 on c1.guest_id = c2.guest_id "+
            "left join rooms c3 on c1.room_id = c3.room_id " +
            "order by create_time DESC ")
    List<CommentVo> selectAllComments();


    /**
     * -- 查询所有一级评论及其回复
     * SELECT c1.id AS comment_id, c1.content AS comment_content, c2.id AS reply_id, c2.content AS reply_content
     * FROM comments c1
     * LEFT JOIN comments c2 ON c1.id = c2.parent_id
     * WHERE c1.parent_id IS NULL;
     */
//    二级评论方法
//    @Select("select c1.comment_id,c1.comment as comment_content, c2.comment_id as reply_id, c2.comment as reply_content " +
//            "from comments c1 " +
//            "left join comments c2 on c1.comment_id = c2.parent_id " +
//            "where c1.parent_id is null")
//    List<Object> allComment();

}




